{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "from keras.models import Model\n",
    "from keras.layers import Input\n",
    "from keras.layers.convolutional import Conv2D\n",
    "from keras.layers.pooling import MaxPooling2D, AveragePooling2D\n",
    "from keras.layers.normalization import BatchNormalization\n",
    "from keras import backend as K"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def format_decimal(arr, places=8):\n",
    "    return [round(x * 10**places) / 10**places for x in arr]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### pipeline 11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'weights': [{'data': [-0.19388144, -0.19001575, -0.09731586, -0.20392132, 0.52195872, -0.83362812, -0.41735846, -0.4328975, 0.43380002, -0.20766614, -0.7679554, -0.13498828, 0.81221727, -0.7898045, -0.40313372, -0.7062533, -0.36001102, -0.51930514, -0.55812459, -0.95621084, -0.7941376, -0.99856347, -0.60933887, -0.94050465, 0.85984897, 0.06789528, -0.53725708, 0.59134801, -0.39666546, 0.89661752, 0.22910667, 0.61376976, -0.93875236, 0.65402795, 0.1428073, -0.33452599, 0.90611209, 0.159331, 0.87139424, 0.19330817, 0.03415369, 0.48841223, -0.21573357, 0.76914577, 0.58694333, -0.75001631, 0.68083019, 0.91907979, -0.47421929, 0.87404362, -0.71564904, 0.8386454, 0.74925525, -0.29101729, -0.79739646, -0.72944405, 0.61943975, 0.63606455, 0.83240359, 0.00771567, -0.08668039, -0.66323566, -0.6568711, -0.08186235, 0.03399537, 0.2039048, 0.90356066, 0.09368177, -0.03195618, -0.61772711, 0.3717567, 0.88043978], 'shape': [3, 3, 2, 4]}, {'data': [-0.2865981, -0.73947835, 0.60869202, -0.19931851], 'shape': [4]}], 'input': {'data': [-0.19388144, -0.19001575, -0.09731586, -0.20392132, 0.52195872, -0.83362812, -0.41735846, -0.4328975, 0.43380002, -0.20766614, -0.7679554, -0.13498828, 0.81221727, -0.7898045, -0.40313372, -0.7062533, -0.36001102, -0.51930514, -0.55812459, -0.95621084, -0.7941376, -0.99856347, -0.60933887, -0.94050465, 0.85984897, 0.06789528, -0.53725708, 0.59134801, -0.39666546, 0.89661752, 0.22910667, 0.61376976, -0.93875236, 0.65402795, 0.1428073, -0.33452599, 0.90611209, 0.159331, 0.87139424, 0.19330817, 0.03415369, 0.48841223, -0.21573357, 0.76914577, 0.58694333, -0.75001631, 0.68083019, 0.91907979, -0.47421929, 0.87404362, -0.71564904, 0.8386454, 0.74925525, -0.29101729, -0.79739646, -0.72944405, 0.61943975, 0.63606455, 0.83240359, 0.00771567, -0.08668039, -0.66323566, -0.6568711, -0.08186235, 0.03399537, 0.2039048, 0.90356066, 0.09368177, -0.03195618, -0.61772711, 0.3717567, 0.88043978, 0.52451349, 0.65639836, -0.11815827, -0.5935923, -0.81520379, -0.11653893, -0.30373012, 0.23333123, -0.76455189, 0.7411041, -0.73334668, -0.64117813, -0.52816925, 0.72175618, -0.20883569, 0.91916448, -0.94918497, -0.73977154, 0.48306794, 0.53576453, 0.42844311, -0.40423223, 0.58140529, -0.80551698, -0.58405683, -0.77108705, -0.5249814, -0.3140284, -0.90075427, -0.27333001, -0.01088696, -0.78774164, -0.11778349, 0.57551081, 0.51202998, 0.30001746, -0.09656189, -0.91341054, -0.45544411, -0.55559633, 0.67950395, 0.85886773, 0.55813851, 0.95827444, -0.69859671, 0.93609409, -0.33106889, 0.85475746, 0.04118771, -0.50402696, -0.75944605, -0.39477802, -0.16001778, 0.02823298, -0.23748051, 0.5115979], 'shape': [8, 8, 2]}, 'expected': {'data': [0.06974199, 1.22076452, 1.82424057, 0.7483421, 0.06974199, 1.20541334, 1.62150133, 1.17863536, 0.30199394, 0.37979802, 1.5488044, 1.49579191, 0.84266365, 0.13482136, 1.24629283, 0.6912452, 1.08216572, 0.37153888, 1.31310272, 0.84929281, 0.27304667, 1.38722563, 1.40454769, 0.7483421, 0.24490587, 1.27858114, 1.15411615, 1.25485075, 0.47715783, 0.37597644, 1.19949126, 1.28506064, 0.42179984, 0.34311506, 0.72356665, 0.32109338, 0.11980587, 0.34311506, 0.0, 0.06555998, 0.55969381, 0.22475854, 0.3414948, 0.82348567, 0.53155297, 0.13528675, 0.13688047, 1.1318965, 0.17516388, 0.0, 0.0, 0.30841088, 0.22057109, 0.20829371, 0.27007097, 0.0, 0.40048063, 0.22537969, 0.59105206, 0.00139774, 0.35638911, 0.0, 0.59383386, 0.8933332, 0.35638911, 0.0, 0.92993259, 0.82348567, 0.17073184, 0.0, 0.60969913, 0.4473317, 0.34426671, 0.0583894, 0.66471696, 0.4473317, 0.35344443, 0.75945652, 0.86008, 0.05082553, 0.0, 0.0, 0.45695338, 0.06984752, 0.37820315, 0.0, 0.80457485, 0.0, 0.69989324, 0.0, 0.66324008, 0.4473317, 0.32169005, 0.34720582, 0.49270964, 0.4473317, 0.0, 1.03118694, 0.32507342, 0.04942778], 'shape': [5, 5, 4]}}\n"
     ]
    }
   ],
   "source": [
    "data_in_shape = (8, 8, 2)\n",
    "\n",
    "conv_0 = Conv2D(4, 3, 3, activation='relu', border_mode='valid', subsample=(1, 1), dim_ordering='tf', bias=True)\n",
    "pool_0 = AveragePooling2D(pool_size=(2, 2), strides=(1, 1), border_mode='valid', dim_ordering='tf')\n",
    "\n",
    "input_layer = Input(shape=data_in_shape)\n",
    "x = conv_0(input_layer)\n",
    "output_layer = pool_0(x)\n",
    "model = Model(input=input_layer, output=output_layer)\n",
    "\n",
    "np.random.seed(12000)\n",
    "data_in = 2 * np.random.random(data_in_shape) - 1\n",
    "\n",
    "# set weights to random (use seed for reproducibility)\n",
    "weights = []\n",
    "for i, w in enumerate(model.get_weights()):\n",
    "    np.random.seed(12000 + i)\n",
    "    weights.append(2 * np.random.random(w.shape) - 1)\n",
    "model.set_weights(weights)\n",
    "\n",
    "result = model.predict(np.array([data_in]))\n",
    "\n",
    "print({\n",
    "    'input': {'data': format_decimal(data_in.ravel().tolist()), 'shape': list(data_in_shape)},\n",
    "    'weights': [{'data': format_decimal(weights[i].ravel().tolist()), 'shape': list(weights[i].shape)} for i in range(len(weights))],\n",
    "    'expected': {'data': format_decimal(result[0].ravel().tolist()), 'shape': list(result[0].shape)}\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
